using AnimationPage.Extensions;
using Avalonia;
using Avalonia.Controls;

namespace AnimationPage.AttachedProperties;

/// <summary>
/// 从右边滑出的动画
/// 基于margin
/// </summary>
public sealed class AnimateSlideInFromRightProperty : AnimateBaseProperty<AnimateSlideInFromRightProperty>
{
    public static readonly AvaloniaProperty<bool> ValueProperty =
        AvaloniaProperty.RegisterAttached<AvaloniaObject, bool>
            ("Value", typeof(AnimateSlideInFromRightProperty), coerce: OnCallBack);

    private static bool OnCallBack(AvaloniaObject arg1, bool arg2) => OnCallBack(ValueProperty, arg1, arg2);

    public static bool GetValue(AvaloniaObject d) => d.GetValue<bool>(ValueProperty);

    public static void SetValue(AvaloniaObject d, bool value) => d.SetValue(ValueProperty, value);

    protected override async void DoAnimation(Control element, bool value, bool firstLoad)
    {
        if (value)
            await element.SlideAndFadeInAsync(AnimationSlideInDirection.Right, firstLoad, firstLoad ? 0 : 0.3f,
                keepMargin: true);
        else
            await element.SlideAndFadeOutAsync(AnimationSlideInDirection.Right, firstLoad ? 0 : 0.3f, keepMargin: true,
                firstLoad: firstLoad);
    }
}